package com.service.impl;


import com.dao.ClerkDao;
import com.dao.impl.ClerkDaoImpl;
import com.entity.Clerk;
import com.result.QueryResult;
import com.service.ClerkService;
import com.util.DBUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author lenovo
 */
public class ClerkServiceImpl implements ClerkService {
    //声明一个CouponDao的对象
    ClerkDao clerkDao =new ClerkDaoImpl();
    @Override
    public QueryResult selectByPage(Integer offset, Integer size) {
        return clerkDao.selectByPage(offset,size);
    }

    @Override
    public Clerk add(Clerk clerk) throws SQLException {
        Integer insert=clerkDao.insert(clerk);

        //查询此次添加后生成的id
        String lastInsertId="select Last_INSERT_ID()";
        PreparedStatement preparedStatement= DBUtil.createPreparedStatement(lastInsertId);
        ResultSet resultSet= DBUtil.execQuery(preparedStatement);
        if(resultSet.next()){
            clerk.setId(resultSet.getInt(1));
        }
        return clerk;
    }

    @Override
    public Integer delete(Integer id) {
        return clerkDao.delete(id);
    }

    @Override
    public void update(Clerk clerk) {
        clerkDao.update(clerk);
    }

    @Override
    public QueryResult searchLikeName(String searchName,Integer offset, Integer size) {
        return clerkDao.searchLikeName(searchName,offset,size);
    }
}
